package com.ym.manager.admin.redis;

import com.alibaba.fastjson.JSONObject;
import com.ym.datasource.redis.JedisClient;
import com.ym.utils.JsonUtil;
import com.ym.utils.StringHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service

public class RedsiInfoService {
    @Autowired
    private JedisClient jedisClient;
    /**
     * @ 名称:获取用户未读消息
     * @ 创建人: dhf
     * @ 参数： null
     * @ 创建时间: 2022/5/11
     * @ 修改人和其它信息
     * @ 返回值:
     */
    public List<MessageResponseBean> getUnReadMessage(Long uid) {
//        Redis.key.del(UserKey.unReadMessageKey(McodeHandler.to32Radix(uid)));
        List<MessageResponseBean> list = new ArrayList<>();
        String unReadMessageKey = "user:unread:entity:"+uid;
        if (jedisClient.exists(unReadMessageKey)) {
            Map map = jedisClient.getMap(unReadMessageKey);
            List<String> arrayList = new ArrayList<String>(map.values());
            arrayList.forEach(x -> {
                list.add(JsonUtil.jsonToBean(x, MessageResponseBean.class));
            });
        }


        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return list.stream().filter(x -> StringHandler.isNotEmpty(x.getMessageId())).filter(x -> distinctByKey(x.getMessageId() + "_" + x.getCode(), seen)).collect(Collectors.toList());
    }
    public static boolean distinctByKey(String messageId, Map<Object, Boolean> seen) {
        return seen.putIfAbsent(messageId, Boolean.TRUE) == null;
    }

}
